package com.xoa.dev.flowStatistics.service.impl;

import com.xoa.dev.flowStatistics.dao.StatisticsMapper;
import com.xoa.dev.flowStatistics.model.Statistics;
import com.xoa.dev.flowStatistics.service.StatisticsService;
import com.xoa.service.WFE.WFEFactory.WFEFormData;
import com.xoa.util.HtmlUtil;
import com.xoa.util.common.wrapper.BaseWrappers;
import com.xoa.util.page.PageParams;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private StatisticsMapper statisticsMapper;
    @Autowired
    private WFEFormData wfeFormData;

    @Override
    public BaseWrappers queryFlowRunList(Integer page, Integer pageSize, Boolean useFlag) {
        List<Integer> list = new ArrayList<>();
        List<Map<Object, Object>> list1 = new ArrayList<>();
        BaseWrappers baseWrapper = new BaseWrappers();
        list.add(233);
        list.add(231);
        list.add(216);
        list.add(171);
        list.add(223);
        list.add(217);
        List<Integer> list2 = new ArrayList();
        Map maps = new HashMap();
        Integer start = (page - 1) * pageSize;
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        maps.put("page", pageParams);
        maps.put("list", list);
        /*list2 = statisticsMapper.selectRunIds(maps);
        Integer count = statisticsMapper.selectCountRunIds(list);
        list1 = statisticsMapper.selectRunIdsByFlowId(list, list2);*/
        Integer count = statisticsMapper.selectCountRunIds(list);
        list2 = statisticsMapper.selectRunIds(maps);
        list1 = statisticsMapper.selectRunIdsByFlowId(list, list2);

        if (list1.size() == 0) {
            baseWrapper.setFlag(false);
            baseWrapper.setStatus(false);
            //  baseWrapper.setDatas(list1);
            return baseWrapper;
        }
        List<Statistics> result = new ArrayList<>();
        for (Map map : list1) {
            //获取表单信息
            Document document = wfeFormData.getFormDocument(Integer.parseInt(map.get("flowId").toString()));
//获取审批内容
            if ((map.get("flowId") != null && map.get("flowId").equals(233)) || (map.get("flowId") != null && map.get("flowId").equals(231))) {
                //   if((map.get("flowId")!=null)){
                StringBuffer buffer = new StringBuffer();
                try {
                buffer.append(HtmlUtil.getDocumentName(document, "总金额小写") + " as flowData1" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "所属项目") + " as flowData2" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "所属部门") + " as flowData3" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "出差小结/报销事由") + " as  flowData4" + ",");
                //   buffer.append(HtmlUtil.getDocumentName(document, "差旅费") + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "报销人") + " as  flowData6" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "经办人") + " as  flowData7" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "结束日期") + " as  flowData8" + ",");
                //  buffer.append(HtmlUtil.getDocumentName(document, "流水号")+" as  flowData10"  + ",");
                buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                }catch (Exception e){

                }
                String sql = buffer.toString();
                String tabelName = "flow_data_" + (map.get("flowId"));
                Integer[] runIds = stringConvertInt(map.get("runId").toString()); // 用,分割
                List<Statistics> flow_list1 = statisticsMapper.selectFlowData(sql, tabelName, runIds);
                if (flow_list1.size() > 0) {
                    // result.add(flow_list1);
                    result.addAll(flow_list1);
                }
                //两个list
            } else if (map.get("flowId") != null && map.get("flowId").equals(216)) {
                StringBuffer buffer = new StringBuffer();
                try {
                buffer.append(HtmlUtil.getDocumentName(document, "报销明细") + " as flowData" + ",");//表格控件对应存储摘要，费用所属项目，费用所属部门，金额，费用所属类型，备注
                buffer.append(HtmlUtil.getDocumentName(document, "报销人") + " as  flowData6" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "经办人") + " as  flowData7" + ",");
                //结束日期字段表单中不存在
                // buffer.append(HtmlUtil.getDocumentName(document, "结束日期") + ",");
                // buffer.append("col_5 "+",");//备注
                //  buffer.append(HtmlUtil.getDocumentName(document, "流水号") +" as  flowData10"  + ",");
                buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                }catch (Exception e){

                }
                String sql = buffer.toString();
                String tabelName = "flow_data_" + (map.get("flowId"));
                Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
                List<Statistics> flow_list2 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
                List<Statistics> list3 = new ArrayList<>();
                if (flow_list2.size() > 0) {
                    StringBuffer buffer1 = new StringBuffer();
                    StringBuffer buffer2 = new StringBuffer();
                    StringBuffer buffer3 = new StringBuffer();
                    StringBuffer buffer4 = new StringBuffer();
                    StringBuffer buffer5 = new StringBuffer();
                    StringBuffer buffer6 = new StringBuffer();
                    for (Statistics statistics : flow_list2) {
                        String data = statistics.getFlowData();
                        if (data != null && !data.equals("")) {
                            if (statistics.getFlowData().indexOf("|") != -1) {
                                data = statistics.getFlowData().substring(0, statistics.getFlowData().indexOf("|"));
                            }
                            String data1[] = data.split("\r\n");//拿到多行里的每行串
                            if (data1.length > 0) {
                                for (String str : data1) {
                                    String data2[] = str.split("`");//得到每行数据
                                    if (data2.length > 0) {
                                        buffer1.append(data2[0].equals("") ? "" : data2[0] + ";;");
                                        buffer2.append(1 < data2.length ? data2[1].equals("") ? "" : data2[1] + ";;" : "");//所属项目)
                                        buffer3.append(2 < data2.length ? data2[2].equals("") ? "" : data2[2] + ";;" : "");//所属部门)
                                        buffer4.append(3 < data2.length ? data2[3].equals("") ? "" : data2[3] + ";;" : "");//金额
                                        buffer5.append(4 < data2.length ? data2[4].equals("") ? "" : data2[4] + ";;" : "");//费用类型
                                        buffer6.append(5 < data2.length ? data2[5].equals("") ? "" : data2[5] + ";;" : "");//备注)
                                    }
                                }
                            }
                            statistics.setFlowData4(buffer1.substring(0, buffer1.length() == 0 ? buffer1.length() : buffer1.length() - 2));//摘要
                            statistics.setFlowData2(buffer2.substring(0, buffer2.length() == 0 ? buffer2.length() : buffer2.length() - 2));//所属项目
                            statistics.setFlowData3(buffer3.substring(0, buffer3.length() == 0 ? buffer3.length() : buffer3.length() - 2));//所属部门
                            statistics.setFlowData1(buffer4.substring(0, buffer4.length() == 0 ? buffer4.length() : buffer4.length() - 2));//金额
                            statistics.setFlowData5(buffer5.substring(0, buffer5.length() == 0 ? buffer5.length() : buffer5.length() - 2));//费用类型
                            statistics.setFlowData9(buffer6.substring(0, buffer6.length() == 0 ? buffer6.length() : buffer6.length() - 2));//备注
                        }
                        buffer1.delete(0, buffer1.length());
                        buffer2.delete(0, buffer2.length());
                        buffer3.delete(0, buffer3.length());
                        buffer4.delete(0, buffer4.length());
                        buffer5.delete(0, buffer5.length());
                        buffer6.delete(0, buffer6.length());
                        list3.add(statistics);
                    }
                    result.addAll(list3);
                }
            } else if ((map.get("flowId") != null && map.get("flowId").equals(171))) {
                StringBuffer buffer = new StringBuffer();
                try {
                buffer.append(HtmlUtil.getDocumentName(document, "汇款金额小写") + " as flowData1" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "项目名称") + " as flowData2" + ",");
                //费用所属部门表单字段不存在
                buffer.append(HtmlUtil.getDocumentName(document, "费用所属部门") + " as flowData3"  + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "汇款用途") + " as  flowData4" + ",");
                //费用类型表单字段不存在
                buffer.append(HtmlUtil.getDocumentName(document, "费用类型") + " as  flowData5"  + ",");
                //报销人表单字段找不到
                buffer.append(HtmlUtil.getDocumentName(document, "收款单位") + " as  flowData6"  + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "用款部门经办人签字") + " as  flowData7" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "汇出日期") + " as  flowData8" + ",");
                //   buffer.append(HtmlUtil.getDocumentName(document, "流水号") + ",");
                buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                }catch (Exception e){

                }
                String sql = buffer.toString();
                String tabelName = "flow_data_" + (map.get("flowId"));
                Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
                List<Statistics> flow_list4 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
                if (flow_list4.size() > 0) {
                    result.addAll(flow_list4);
                }
            } else if ((map.get("flowId") != null && map.get("flowId").equals(223))) {
                StringBuffer buffer = new StringBuffer();
                try {
                buffer.append(HtmlUtil.getDocumentName(document, "汇款列表") + " as flowData" + ",");////表格控件对应存储摘要，费用所属项目，费用所属部门，费用所属类型，金额,备注
                //报销人控件不存在
                // buffer.append(HtmlUtil.getDocumentName(document, "报销人") + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "用款部门经办人签字") + " as  flowData7" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "汇出日期") + " as  flowData8" + ",");
                //  buffer.append(HtmlUtil.getDocumentName(document, "流水号") + ",");
                buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                }catch (Exception e){

                }
                //String sql = buffer.toString().substring(0, buffer.toString().length() - 1);
                String sql = buffer.toString();
                String tabelName = "flow_data_" + (map.get("flowId"));
                Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
                List<Statistics> flow_list5 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
                List<Statistics> list5 = new ArrayList<>();
                if (flow_list5.size() > 0) {
                    StringBuffer buffer1 = new StringBuffer();
                    StringBuffer buffer2 = new StringBuffer();
                    StringBuffer buffer3 = new StringBuffer();
                    StringBuffer buffer4 = new StringBuffer();
                    StringBuffer buffer5 = new StringBuffer();
                    StringBuffer buffer6 = new StringBuffer();
                    for (Statistics statistics : flow_list5) {
                        String data = statistics.getFlowData();
                        if (data != null && !data.equals("")) {
                            if (statistics.getFlowData().indexOf("|") != -1) {
                                data = statistics.getFlowData().substring(0, statistics.getFlowData().indexOf("|"));
                            }
                            String data1[] = data.split("\r\n");//拿到多行里的每行串
                            if (data1.length > 0) {
                                for (String str : data1) {
                                    String data2[] = str.split("`");//得到每行数据
                                    if (data2.length > 0) {
                                        buffer1.append(data2[0].equals("") ? "" : data2[0] + ";;");
                                        buffer2.append(1 < data2.length ? data2[1].equals("") ? "" : data2[1] + ";;" : "");//所属项目)//2
                                        buffer3.append(2 < data2.length ? data2[2].equals("") ? "" : data2[2] + ";;" : "");//所属部门
                                        buffer4.append(3 < data2.length ? data2[3].equals("") ? "" : data2[3] + ";;" : "");//费用类型)
                                        buffer5.append(4 < data2.length ? data2[4].equals("") ? "" : data2[4] + ";;" : "");//金额
                                        buffer.append(6 < data2.length ? data2[6].equals("") ? "" : data2[6] + ";;" : "");//备注
                                    }
                                }
                            }
                            statistics.setFlowData4(buffer1.substring(0, buffer1.length() == 0 ? buffer1.length() : buffer1.length() - 2));//摘要
                            statistics.setFlowData2(buffer2.substring(0, buffer2.length() == 0 ? buffer2.length() : buffer2.length() - 2));//所属项目
                            statistics.setFlowData3(buffer3.substring(0, buffer3.length() == 0 ? buffer3.length() : buffer3.length() - 2));//所属部门
                            statistics.setFlowData1(buffer4.substring(0, buffer4.length() == 0 ? buffer4.length() : buffer4.length() - 2));//费用类型
                            statistics.setFlowData5(buffer5.substring(0, buffer5.length() == 0 ? buffer5.length() : buffer5.length() - 2));//金额
                            statistics.setFlowData9(buffer6.substring(0, buffer6.length() == 0 ? buffer6.length() : buffer6.length() - 2));//备注
                        }
                        buffer1.delete(0, buffer1.length());
                        buffer2.delete(0, buffer2.length());
                        buffer3.delete(0, buffer3.length());
                        buffer4.delete(0, buffer4.length());
                        buffer5.delete(0, buffer5.length());
                        buffer6.delete(0, buffer6.length());

                        list5.add(statistics);
                    }
                    result.addAll(list5);
                }
            } else if ((map.get("flowId") != null && map.get("flowId").equals(217))) {
                StringBuffer buffer = new StringBuffer();
                try {
                    //换算人民币金额控件不存在 金额字段
                    //buffer.append(HtmlUtil.getDocumentName(document, "换算人民币金额") + ",");
                    buffer.append(HtmlUtil.getDocumentName(document, "所属项目") + " as flowData2" + ",");
                    buffer.append(HtmlUtil.getDocumentName(document, "所属部门") + " as flowData3" + ",");
                    buffer.append(HtmlUtil.getDocumentName(document, "汇款用途") + " as  flowData4" + ",");
                    //找不到该字段
                    //buffer.append(HtmlUtil.getDocumentName(document, "费用类型") + ",");
                    //报销人找不到字段
                    // buffer.append(HtmlUtil.getDocumentName(document, "报销人") + ",");
                    //经办人找不到字段表单中
                    //buffer.append(HtmlUtil.getDocumentName(document, "用款部门经办人签字") + ",");
                    buffer.append(HtmlUtil.getDocumentName(document, "汇出日期") + " as  flowData8" + ",");
                    //      buffer.append(HtmlUtil.getDocumentName(document, "流水号") + ",");
                    buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                }catch (Exception e){

                }
                String sql = buffer.toString();
                String tabelName = "flow_data_" + (map.get("flowId"));
                Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
                List<Statistics> flow_list6 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
                if (flow_list6.size() > 0) {
                    result.addAll(flow_list6);
                }
            }

        }
        // List<List>lists=statisticsMapper.selectFlowData(sql,LI);
        //  Document document=getDocument
        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        baseWrapper.setDatas(result);
        maps.remove("page");
        baseWrapper.setTotal(count);
        return baseWrapper;
        // baseWrapper.setTotal(lis);
    }

    @Override
    public Map queryFlowRuns() {
        List<Integer> list = new ArrayList<>();
        List<Map<Object, Object>> list1 = new ArrayList<>();
        BaseWrappers baseWrapper = new BaseWrappers();

        list.add(233);
        //list.add(231);
        //list.add(216);
        list.add(171);
        //list.add(223);
        //list.add(217);
        Map<String,Double> map1 = new HashMap();
        Map<String,Double> map2 = new HashMap();
        Map <String,Double>map3 = new HashMap();
        List<Integer> list2 = new ArrayList();
        Map maps = new HashMap();
        maps.put("list", list);
        Map resultmap = new HashMap();
        /*创建金额数组*/
        String[] money=new String[2000];
        /*创建项目数组*/
        String[] item=new String[2000];
        //list2=statisticsMapper.selectRunIds(maps);
        //图形化统计
        list1 = statisticsMapper.selectRunIdsByFlowIds(list);
        if (list1.size() == 0) {
            baseWrapper.setFlag(false);
            baseWrapper.setStatus(false);
            //  baseWrapper.setDatas(list1);
            return resultmap;
        }
        List<Statistics> result = new ArrayList<>();
        for (Map map : list1) {
            //获取表单信息
            Document document = wfeFormData.getFormDocument(Integer.parseInt(map.get("flowId").toString()));
            //获取审批内容

            if ((map.get("flowId") != null && map.get("flowId").equals(233)) || (map.get("flowId") != null && map.get("flowId").equals(231))) {
                //   if((map.get("flowId")!=null)){
                StringBuffer buffer = new StringBuffer();
                try {
                    buffer.append(HtmlUtil.getDocumentName(document, "总金额小写") + " as flowData1" + ",");
                    buffer.append(HtmlUtil.getDocumentName(document, "所属项目") + " as flowData2" + ",");
                    buffer.append(HtmlUtil.getDocumentName(document, "所属部门") + " as flowData3" + ",");
                    buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                }catch (Exception e){

                }

                //  buffer.append(HtmlUtil.getDocumentName(document, "出差小结/报销事由") + " as  flowData4" + ",");
                //费用类型字段不存在
                //   buffer.append(HtmlUtil.getDocumentName(document, "差旅费") + ",");
                //  buffer.append(HtmlUtil.getDocumentName(document, "报销人") + " as  flowData6" + ",");
                //buffer.append(HtmlUtil.getDocumentName(document, "经办人") + " as  flowData7" + ",");
                // buffer.append(HtmlUtil.getDocumentName(document, "结束日期") + " as  flowData8" + ",");
                //  buffer.append(HtmlUtil.getDocumentName(document, "流水号")+" as  flowData10"  + ",");
                //   buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
                String sql = buffer.toString();
                String tabelName = "flow_data_" + (map.get("flowId"));
                Integer[] runIds = stringConvertInt(map.get("runId").toString()); // 用,分割
                List<Statistics> flow_list1 = statisticsMapper.selectFlowData(sql, tabelName, runIds);
                if (flow_list1.size() > 0) {

                    /*for (Statistics statistics : flow_list1) {
                        if (map1.containsKey(statistics.getFlowData2())) {
                            map1.put(statistics.getFlowData2(),map1.get(statistics.getFlowData2()) +Double.valueOf( statistics.getFlowData1()));

                        } else {
                            map1.put(statistics.getFlowData2(), Double.valueOf(statistics.getFlowData1()));
                        }
                        if (map2.containsKey(statistics.getFlowData3())) {
                            map2.put(statistics.getFlowData3(), map2.get(statistics.getFlowData3()) + Double.valueOf(statistics.getFlowData1()));
                        } else {
                            map2.put(statistics.getFlowData3(),Double.valueOf( statistics.getFlowData1()));
                        }
                        if (map3.containsKey(statistics.getFlowData5())) {
                            map3.put(statistics.getFlowData5(), map2.get(statistics.getFlowData5())+Double.valueOf(statistics.getFlowData1()));
                        } else {
                            map3.put(statistics.getFlowData5(), Double.valueOf(statistics.getFlowData1()));
                        }
                        resultmap.put("money",statistics.getFlowData1());//金额统计
                        resultmap.put("item",statistics.getFlowData2());//项目
                    }*/
                    int i= result.size()==0?0:result.size();
                    for (Statistics statistics : flow_list1) {
                        money[i]=statistics.getFlowData1();
                        /*将月份放入数组*/
                        item[i]=statistics.getFlowData2();
                        i++;
                    }
                    result.addAll(flow_list1);
                }
                //result.addAll(flow_list1);

            //两个list
        } else if (map.get("flowId") != null && map.get("flowId").equals(216)) {
            StringBuffer buffer = new StringBuffer();
            try {
            buffer.append(HtmlUtil.getDocumentName(document, "报销明细") + " as flowData" + ",");//表格控件对应存储摘要，费用所属项目，费用所属部门，金额，费用所属类型，备注
            buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
            }catch (Exception e){

            }
            // buffer.append(HtmlUtil.getDocumentName(document, "报销人") + " as  flowData6" + ",");
            // buffer.append(HtmlUtil.getDocumentName(document, "经办人") + " as  flowData7" + ",");
            //结束日期字段表单中不存在
            // buffer.append(HtmlUtil.getDocumentName(document, "结束日期") + ",");
            // buffer.append("col_5 "+",");//备注
            //  buffer.append(HtmlUtil.getDocumentName(document, "流水号") +" as  flowData10"  + ",");
            //   buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段

            String sql = buffer.toString();
            String tabelName = "flow_data_" + (map.get("flowId"));
            Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
            List<Statistics> flow_list2 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
            List<Statistics> list3 = new ArrayList<>();
            if (flow_list2.size() > 0) {
                /*StringBuffer buffer1 = new StringBuffer();
                StringBuffer buffer2 = new StringBuffer();
                StringBuffer buffer3 = new StringBuffer();
                StringBuffer buffer4 = new StringBuffer();
                StringBuffer buffer5 = new StringBuffer();
                StringBuffer buffer6 = new StringBuffer();
                for (Statistics statistics : flow_list2) {
                    String data = statistics.getFlowData();
                    if (data != null && !data.equals("")) {
                        if (statistics.getFlowData().indexOf("|") != -1) {
                            data = statistics.getFlowData().substring(0, statistics.getFlowData().indexOf("|"));
                        }
                        String data1[] = data.split("\r\n");//拿到多行里的每行串
                        if (data1.length > 0) {
                            for (String str : data1) {
                                String data2[] = str.split("`");//得到每行数据
                                if (data2.length > 0) {
                                  //  buffer1.append(data2[0].equals("") ? "" : data2[0] + ";;");
                             if(data2.length>1&&!data2[1].equals("")){
                                 if(map1.containsKey(data2[1])){
                                     map1.put(data2[1],map1.get(data2[1])+Double.valueOf(3< data2.length ? Double.valueOf(data2[3]).equals("") ? 0:Double.valueOf(data2[3]):0));
                                 }
                             }
                                  //map1.put(1 < data2.length ? data2[1].equals("") ? "" : data2[1]:"",3< data2.length ? data2[3].equals("") ? "":data2[3]:"");
                                    //map2.put(2 < data2.length ? data2[2].equals("") ? "" : data2[2]:"",3 < data2.length ? data2[3].equals("") ? "":data2[3]:"");
                                    //map1.put(4 < data2.length ? data2[4].equals("") ? "" : data2[4]:"",3 < data2.length ? data2[3].equals("") ? "":data2[3]:"");

                                    // buffer6.append(5 < data2.length ? data2[5].equals("")?"":data2[5] + ";;" : "");//备注)
                                }
                            }
                        }
                        statistics.setFlowData4(buffer1.substring(0, buffer1.length() == 0 ? buffer1.length() : buffer1.length() - 2));//摘要
                        statistics.setFlowData2(buffer2.substring(0, buffer2.length() == 0 ? buffer2.length() : buffer2.length() - 2));//所属项目
                        statistics.setFlowData3(buffer3.substring(0, buffer3.length() == 0 ? buffer3.length() : buffer3.length() - 2));//所属部门
                        statistics.setFlowData1(buffer4.substring(0, buffer4.length() == 0 ? buffer4.length() : buffer4.length() - 2));//金额
                        statistics.setFlowData5(buffer5.substring(0, buffer5.length() == 0 ? buffer5.length() : buffer5.length() - 2));//费用类型
                        statistics.setFlowData9(buffer6.substring(0, buffer6.length() == 0 ? buffer6.length() : buffer6.length() - 2));//备注
                    }
                    buffer1.delete(0, buffer1.length());
                    buffer2.delete(0, buffer2.length());
                    buffer3.delete(0, buffer3.length());
                    buffer4.delete(0, buffer4.length());
                    buffer5.delete(0, buffer5.length());
                    buffer6.delete(0, buffer6.length());
                    list3.add(statistics);
                    resultmap.put("money",statistics.getFlowData1());//金额统计
                    resultmap.put("item",statistics.getFlowData2());//项目
                }*/
                int i= result.size()==0?0:result.size();
                for (Statistics statistics : flow_list2) {
                    money[i]=statistics.getFlowData1();
                    /*将月份放入数组*/
                    item[i]=statistics.getFlowData2();
                    i++;
                }
                result.addAll(list3);
            }
        } else if ((map.get("flowId") != null && map.get("flowId").equals(171))) {
            StringBuffer buffer = new StringBuffer();
            try {
            buffer.append(HtmlUtil.getDocumentName(document, "汇款金额小写") + " as flowData1" + ",");
            buffer.append(HtmlUtil.getDocumentName(document, "项目名称") + " as flowData2" + ",");
            //表单字段不存在
            //buffer.append(HtmlUtil.getDocumentName(document, "费用所属部门") + ",");
            buffer.append(HtmlUtil.getDocumentName(document, "汇款用途") + " as  flowData4" + ",");
            //表单字段不存在
            //buffer.append(HtmlUtil.getDocumentName(document, "费用类型") + ",");
            //报销人表单字段找不到
            // buffer.append(HtmlUtil.getDocumentName(document, "报销人") + ",");
            buffer.append(HtmlUtil.getDocumentName(document, "用款部门经办人签字") + " as  flowData7" + ",");
            buffer.append(HtmlUtil.getDocumentName(document , "汇出日期") + " as  flowData8" + ",");
            //   buffer.append(HtmlUtil.getDocumentName(document, "流水号") + ",");
            buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
            }catch (Exception e){

            }
            String sql = buffer.toString();
            String tabelName = "flow_data_" + (map.get("flowId"));
            Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
            List<Statistics> flow_list4 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
            if (flow_list4.size() > 0) {

                int i= result.size()==0?0:result.size();
                for (Statistics statistics : flow_list4) {
                    money[i]=statistics.getFlowData1();
                    /*将月份放入数组*/
                    item[i]=statistics.getFlowData2();
                    i++;
                }
                result.addAll(flow_list4);
            }

        } else if ((map.get("flowId") != null && map.get("flowId").equals(223))) {
            StringBuffer buffer = new StringBuffer();
            try {
            buffer.append(HtmlUtil.getDocumentName(document, "汇款列表") + " as flowData" + ",");////表格控件对应存储摘要，费用所属项目，费用所属部门，费用所属类型，金额,备注
            //报销人控件不存在
            // buffer.append(HtmlUtil.getDocumentName(document, "报销人") + ",");
            buffer.append(HtmlUtil.getDocumentName(document, "用款部门经办人签字") + " as  flowData7" + ",");
            buffer.append(HtmlUtil.getDocumentName(document, "汇出日期") + " as  flowData8" + ",");

            //  buffer.append(HtmlUtil.getDocumentName(document, "流水号") + ",");
            buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
            }catch (Exception e){

            }
            //String sql = buffer.toString().substring(0, buffer.toString().length() - 1);
            String sql = buffer.toString();
            String tabelName = "flow_data_" + (map.get("flowId"));
            Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
            List<Statistics> flow_list5 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
            List<Statistics> list5 = new ArrayList<>();
            if (flow_list5.size() > 0) {/*
                StringBuffer buffer1 = new StringBuffer();
                StringBuffer buffer2 = new StringBuffer();
                StringBuffer buffer3 = new StringBuffer();
                StringBuffer buffer4 = new StringBuffer();
                StringBuffer buffer5 = new StringBuffer();
                StringBuffer buffer6 = new StringBuffer();
                for (Statistics statistics : flow_list5) {
                    String data = statistics.getFlowData();
                    if (data != null && !data.equals("")) {
                        if (statistics.getFlowData().indexOf("|") != -1) {
                            data = statistics.getFlowData().substring(0, statistics.getFlowData().indexOf("|"));
                        }
                        String data1[] = data.split("\r\n");//拿到多行里的每行串
                        if (data1.length > 0) {
                            for (String str : data1) {
                                String data2[] = str.split("`");//得到每行数据
                                if (data2.length > 0) {
                                    buffer1.append(data2[0].equals("") ? "" : data2[0] + ";;");
                                    buffer2.append(1 < data2.length ? data2[1].equals("") ? "" : data2[1] + ";;" : "");//所属项目)//2
                                    buffer3.append(2 < data2.length ? data2[2].equals("") ? "" : data2[2] + ";;" : "");//所属部门
                                    buffer4.append(3 < data2.length ? data2[3].equals("") ? "" : data2[3] + ";;" : "");//费用类型)
                                    buffer5.append(4 < data2.length ? data2[4].equals("") ? "" : data2[4] + ";;" : "");//金额
                                    buffer.append(6 < data2.length ? data2[6].equals("") ? "" : data2[6] + ";;" : "");//备注
                                }
                            }
                        }
                        statistics.setFlowData4(buffer1.substring(0, buffer1.length() == 0 ? buffer1.length() : buffer1.length() - 2));//摘要
                        statistics.setFlowData2(buffer2.substring(0, buffer2.length() == 0 ? buffer2.length() : buffer2.length() - 2));//所属项目
                        statistics.setFlowData3(buffer3.substring(0, buffer3.length() == 0 ? buffer3.length() : buffer3.length() - 2));//所属部门
                        statistics.setFlowData1(buffer4.substring(0, buffer4.length() == 0 ? buffer4.length() : buffer4.length() - 2));//费用类型
                        statistics.setFlowData5(buffer5.substring(0, buffer5.length() == 0 ? buffer5.length() : buffer5.length() - 2));//金额
                        statistics.setFlowData9(buffer6.substring(0, buffer6.length() == 0 ? buffer6.length() : buffer6.length() - 2));//备注
                    }
                    buffer1.delete(0, buffer1.length());
                    buffer2.delete(0, buffer2.length());
                    buffer3.delete(0, buffer3.length());
                    buffer4.delete(0, buffer4.length());
                    buffer5.delete(0, buffer5.length());
                    buffer6.delete(0, buffer6.length());

                    list5.add(statistics);

                    resultmap.put("money",statistics.getFlowData1());//金额统计
                    resultmap.put("item",statistics.getFlowData2());//项目
                }*/

                int i= result.size()==0?0:result.size();
                for (Statistics statistics : flow_list5) {
                    money[i]=statistics.getFlowData1();
                    /*将月份放入数组*/
                    item[i]=statistics.getFlowData2();
                    i++;
                }
                result.addAll(list5);
            }
        } else if ((map.get("flowId") != null && map.get("flowId").equals(217))) {
            StringBuffer buffer = new StringBuffer();
            //换算人民币金额控件不存在 金额字段
            //buffer.append(HtmlUtil.getDocumentName(document, "换算人民币金额") + ",");
                try {
                buffer.append(HtmlUtil.getDocumentName(document, "所属项目") + " as flowData2" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "所属部门") + " as flowData3" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "汇款用途") + " as  flowData4" + ",");
                buffer.append(HtmlUtil.getDocumentName(document, "汇出日期") + " as  flowData8" + ",");
                }catch (Exception e){

                }
            //找不到该字段
            //buffer.append(HtmlUtil.getDocumentName(document, "费用类型") + ",");
            //报销人找不到字段
            // buffer.append(HtmlUtil.getDocumentName(document, "报销人") + ",");
            //经办人找不到字段表单中
            //buffer.append(HtmlUtil.getDocumentName(document, "用款部门经办人签字") + ",");

            //      buffer.append(HtmlUtil.getDocumentName(document, "流水号") + ",");
            buffer.append("run_id as flowData10");//用runId代替表单控件flowID字段
            String sql = buffer.toString();
            String tabelName = "flow_data_" + (map.get("flowId"));
            Integer[] runIds1 = stringConvertInt(map.get("runId").toString()); // 用,分割
            List<Statistics> flow_list6 = statisticsMapper.selectFlowData(sql, tabelName, runIds1);
            if (flow_list6.size() > 0) {

                int i= result.size()==0?0:result.size();
                for (Statistics statistics : flow_list6) {
                    money[i]=statistics.getFlowData1();
                    /*将月份放入数组*/
                    item[i]=statistics.getFlowData2();
                    i++;
                }
                result.addAll(flow_list6);
            }
        }

    }
    //  List<List>lists=statisticsMapper.selectFlowData(sql,LI);
    //  Document document=getDocument
        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        baseWrapper.setDatas(result);
        maps.remove("page");
        //baseWrapper.setTotal(count);
        /*创建金额数组*/
        String[] moneys=new String[result.size()];
        /*创建项目数组*/
        String[] items=new String[result.size()];
        for (int i=0;i<result.size();i++){
            moneys[i] = money[i];
            items[i] = item[i];
        }
        resultmap.put("money",moneys);
        resultmap.put("item",items);
        return resultmap;
    // baseWrapper.setTotal(lis);
}

    private Integer[] stringConvertInt(String value) {
        String[] valueArr = value.split(",");
        Integer[] intArr = new Integer[valueArr.length];
        for (int i = 0; i < valueArr.length; i++) {
            intArr[i] = Integer.parseInt(valueArr[i]);
        }
        return intArr;
    }
}
